package com.wonderGirls.swGirls2.offer;

import android.app.Activity;
import android.content.Context;

import com.dianle.Dianle;
import com.dianle.GetTotalMoneyListener;
import com.dianle.GetUpdateMessageListener;
import com.dianle.SpendMoneyListener;
import com.wonderGirls.swGirls2.MainActivity;
import com.wonderGirls.swGirls2.util.ResourceManager;

public class DianJoyOfferUtil implements IOffer, GetTotalMoneyListener,
		SpendMoneyListener, GetUpdateMessageListener {
	private boolean isInited = false;

	@Override
	public void init(Context context) {
		if (!isInited) {
			isInited = true;
			Dianle.initDianleContext((Activity) context,
					"44eb7b217b9fae1b92a84fabbf9fe10a");
			Dianle.setCustomActivity("com.wonderGirls.swGirls2.dl.ShowImageView");
			// MyView所在的包和类名，MyView要继承DianleOfferActivity。如
			Dianle.setCustomService("com.wonderGirls.swGirls2.dl.ShowImgService");
			// MyService所在的包和类名，MyService要继承DianleOfferHelpService
			Dianle.setUpdateOnlyWifi(false);
			Dianle.getUpdateMessage(this);
		}
	}

	@Override
	public void dispose(Context context) {
	}

	@Override
	public void showOffer(Context context) {
		Dianle.showOffers();
	}

	@Override
	public void updateCoins(Context context) {
		Dianle.getTotalMoney(this);
	}

	@Override
	public void getTotalMoneyFailed(String arg0) {
	}

	@Override
	public void getTotalMoneySuccessed(String arg0, long bonus) {
		if (bonus > 0) {
			try {
				ResourceManager.currentGold += bonus;
				ResourceManager.saveGold();
				// 直接消费
				Dianle.spendMoney((int) bonus, this);
				MainActivity.instance.updateGold();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	@Override
	public void spendMoneyFailed(String arg0) {
	}

	@Override
	public void spendMoneySuccess(long arg0) {
	}

	@Override
	public void getGetUpdateMessageFailed(String arg0) {
	}

	@Override
	public void getGetUpdateMessageSuccessed(String appUrl,
			String newVersionMessage) {
		// 开始下载，完成后提示安装.
		Dianle.autoUpdate();
	}
}
